package com.epam.web_project.bank.command.admin_commands;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ApproveUserCommand implements Command {

	//admin approves a user
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO=daoFactory.getCustomerDAO();
		Customer customer=new Customer();
		customer.setId(Integer.valueOf(request.getParameter("userId")));
		HttpSession session = request.getSession();
		Locale locale=request.getLocale();
		ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
		if(customerDAO.approve(customer)) {
			session.setAttribute("message", bundle.getString("java.user_approved"));
		} else {
			session.setAttribute("message", bundle.getString("java.user_approval_err"));
		}
		return props.getProperty("show_admin_main_command");
	}

}
